﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
//using Printing.DBUtility;
using System.Data.SqlClient;

namespace MINERP.DAL.Frontier
{
    public class ProntierDAL
    {

        public static List<int> GetListOrderProductId(int printerId, string ctmName,string productName,DateTime startTime,DateTime endTime,int orderProductId)
        {
            StringBuilder  sql = new StringBuilder("select distinct a.orderproductid from  orderinfo a inner join orderfile b on a.orderproductid =b.orderproductid where b.status =3 and b.printerid ={0} ");
            if (startTime.ToString() != "0001-1-1 0:00:00")
            {
                sql.Append(" and b.createtime >='{1}'");
            }
            if (endTime.ToString() != "0001-1-1 0:00:00")
            {
                sql.Append(" and b.createtime <='{2}'");
            }
            if (!string.IsNullOrEmpty(ctmName))
            {
                sql.Append(" and a.CtmName like '%{3}%'");
            }
            if (!string.IsNullOrEmpty(productName))
            {
                sql.Append(" and a.OrderProductName like '%{4}%'");
            }
            if (orderProductId !=0)
            {
                sql.Append(" and a.orderproductid = {5}");
            }
            string sqlStr = string.Format(sql.ToString(), printerId,startTime,endTime,ctmName,productName,orderProductId);
            return DataBaseAccess(sqlStr);
        }

        private static List<int> DataBaseAccess(string sqlstr)
        {

            List<int> list = new List<int>();
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                DbCommand cmd = db.GetSqlStringCommand(sqlstr);
                IDataReader rdr = db.ExecuteReader(cmd);
                while (rdr.Read())
                {
                    list.Add(rdr.GetInt32(0));
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return list;
        }
    }
}
